package newx.utils
{
	import engine.net.mgr.SystemReturn;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.net.LocalConnection;
	import flash.utils.getTimer;
	
	import newx.utils.debug.IO;

	public class MemoryOptimise
	{
		public function MemoryOptimise()
		{
		}
		
		static public function gc():void
		{
			try
			{
				var conn1:LocalConnection	=	new LocalConnection();
				var conn2:LocalConnection	=	new LocalConnection();
				conn1.connect("dw_name1");
				conn2.connect("d2_name2");
			}
			catch(e:Error)
			{
				IO.traceLog("force gc");
			}
		}
		
		static public function stopMovie(container:DisplayObjectContainer,isPlay:Boolean = false):void
		{
			if(null	==	container)	return;
			//递归比较慢，顺序不一定。
//			var child:DisplayObject;
//			
//			for(var i:uint=0;	i<container.numChildren;	i++)
//			{
//				child	=	container.getChildAt(i);
//				
//				if(child is MovieClip)
//					MovieClip(child).stop();
//				
//				if(child is DisplayObjectContainer)
//					stopMovie(child as DisplayObjectContainer);
//			}
			
			//循环来实现
			
			var curTime	:	Number		=	getTimer();
			var mcCount:	uint		=	0;
			
			var openList:Array	=	[];
			openList.push(container);
			
			var child:DisplayObject;
			while(openList.length>0)
			{
				child	=	openList.shift();
				
				if(child is MovieClip)
				{
					if(isPlay)
					{
						MovieClip(child).play();
					}else
					{
						MovieClip(child).stop();
					}
					mcCount++;
				}
				
				if(child is DisplayObjectContainer)
				{
					for(var i:uint=0; i<DisplayObjectContainer(child).numChildren;	i++)
					{
						openList.push(DisplayObjectContainer(child).getChildAt(i));
					}
				}
			}
//			if(SystemReturn._isShowLog)
//				IO.traceLog("停止一个DisplayObjectContainer中的所有影片，用时：",getTimer()-curTime," 停MC",mcCount,"个");
		}
	}
}